package info.webest.media.category;

import info.webest.media.Options;
import info.webest.media.activities.MainActivity;
import info.webest.media.files.Storage;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;

public class Category {

	public enum StorageType {
		INTERNAL_STORAGE, EXTERNAL_STORAGE;

		public static StorageType fromInteger(int x) {
			StorageType result = null;
			switch (x) {
			case 0:
				result = INTERNAL_STORAGE;
				break;
			case 1:
				result = EXTERNAL_STORAGE;
				break;
			}
			return result;
		}

		public static int toInteger(StorageType x) {
			int result = -1;
			switch (x) {
			case INTERNAL_STORAGE:
				result = 0;
				break;
			case EXTERNAL_STORAGE:
				result = 1;
				break;
			}
			return result;
		}
	}

	public enum CategoryFolder {
		FOLDER_IMAGE_PICTURE, FOLDER_IMAGE_SCREENSHOTE, FOLDER_IMAGE_CAMERA, // Image
		FOLDER_AUDIO_MUSIC, FOLDER_AUDIO_RADIO, FOLDER_AUDIO_BOOK, FOLDER_AUDIO_RECORDING, // Audio
		FOLDER_VIDEO_MOVIE, // Video
		FOLDER_OTHER_DOWNLOAD, FOLDER_OTHER_BLUETOOTH; // Other

		public static CategoryFolder fromInteger(int x) {
			CategoryFolder result = null;
			switch (x) {
			case 0:
				result = FOLDER_IMAGE_PICTURE;
				break;
			case 1:
				result = FOLDER_IMAGE_SCREENSHOTE;
				break;
			case 2:
				result = FOLDER_IMAGE_CAMERA;
				break;
			case 3:
				result = FOLDER_AUDIO_MUSIC;
				break;
			case 4:
				result = FOLDER_AUDIO_RADIO;
				break;
			case 5:
				result = FOLDER_AUDIO_BOOK;
				break;
			case 6:
				result = FOLDER_AUDIO_RECORDING;
				break;
			case 7:
				result = FOLDER_VIDEO_MOVIE;
				break;
			case 8:
				result = FOLDER_OTHER_DOWNLOAD;
				break;
			case 9:
				result = FOLDER_OTHER_BLUETOOTH;
				break;
			}
			return result;
		}

		public static int toInteger(CategoryFolder x) {
			int result = -1;
			switch (x) {
			case FOLDER_IMAGE_PICTURE:
				result = 0;
				break;
			case FOLDER_IMAGE_SCREENSHOTE:
				result = 1;
				break;
			case FOLDER_IMAGE_CAMERA:
				result = 2;
				break;
			case FOLDER_AUDIO_MUSIC:
				result = 3;
				break;
			case FOLDER_AUDIO_RADIO:
				result = 4;
				break;
			case FOLDER_AUDIO_BOOK:
				result = 5;
				break;
			case FOLDER_AUDIO_RECORDING:
				result = 6;
				break;
			case FOLDER_VIDEO_MOVIE:
				result = 7;
				break;
			case FOLDER_OTHER_DOWNLOAD:
				result = 8;
				break;
			case FOLDER_OTHER_BLUETOOTH:
				result = 9;
				break;
			default:
				result = 3;
				break;
			}
			return result;
		}
	}

	// public static StorageType storage = StorageType.INTERNAL_STORAGE;

	public static void setStorage(StorageType storage) {
		Options.HomeStorage = storage;

		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(MainActivity.getInstance());
		SharedPreferences.Editor editor = preferences.edit();
		editor.putInt(Options.HOME_STORAGE,
				StorageType.toInteger(Options.HomeStorage));
		editor.commit();

	}

	public static String getRootFolder() {
		String root = null;
		switch (Options.HomeStorage) {
		case INTERNAL_STORAGE:
		default:
			// root = Environment.getDataDirectory().getAbsolutePath();
			root = Environment.getExternalStorageDirectory().getAbsolutePath();
			break;
		case EXTERNAL_STORAGE:
			String[] Storages = Storage.getSecondaryStorage();

			if (Storages == null || Storages.length == 0) {
				Options.HomeStorage = StorageType.INTERNAL_STORAGE;
				root = getRootFolder();
				break;
			}
			root = Storages[0];
			break;
		}
		return root;
	}

	public static String setCategoryFolder(CategoryFolder categoryFolder) {

		Options.HomeFolder = categoryFolder;

		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(MainActivity.getInstance());
		SharedPreferences.Editor editor = preferences.edit();
		editor.putInt(Options.HOME_FOLDER,
				CategoryFolder.toInteger(Options.HomeFolder));
		editor.commit();

		String f = null;

		switch (Options.HomeFolder) {

		case FOLDER_IMAGE_PICTURE:
			if (Options.HomeStorage == StorageType.EXTERNAL_STORAGE) {
				f = getRootFolder() + "/Pictures";
			} else {
				f = Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_PICTURES).getAbsolutePath();
			}
			break;

		case FOLDER_IMAGE_SCREENSHOTE:
			if (Options.HomeStorage == StorageType.EXTERNAL_STORAGE) {
				f = getRootFolder() + "/Pictures/Screenshots";
			} else {
				f = Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_PICTURES + "/Screenshots")
						.getAbsolutePath();
			}
			break;

		case FOLDER_IMAGE_CAMERA:
			if (Options.HomeStorage == StorageType.EXTERNAL_STORAGE) {
				f = getRootFolder() + "/DCIM";
			} else {
				f = Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_DCIM).getAbsolutePath();
			}
			break;

		case FOLDER_AUDIO_MUSIC:
			if (Options.HomeStorage == StorageType.EXTERNAL_STORAGE) {
				f = getRootFolder() + "/Music";
			} else {
				f = Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_MUSIC).getAbsolutePath();
			}
			break;

		case FOLDER_AUDIO_RADIO:
			f = "";
			break;

		case FOLDER_AUDIO_BOOK:
			f = getRootFolder() + "/AudioBooks";
			break;

		case FOLDER_AUDIO_RECORDING:
			f = getRootFolder() + "/Recording";
			break;

		case FOLDER_VIDEO_MOVIE:
			if (Options.HomeStorage == StorageType.EXTERNAL_STORAGE) {
				f = getRootFolder() + "/Movies";
			} else {
				f = Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_MOVIES).getAbsolutePath();
			}
			break;

		case FOLDER_OTHER_DOWNLOAD:
			if (Options.HomeStorage == StorageType.EXTERNAL_STORAGE) {
				f = getRootFolder() + "/Download";
			} else {
				f = Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
			}
			break;

		case FOLDER_OTHER_BLUETOOTH:
			f = getRootFolder() + "/bluetooth";
			break;

		default:
			if (Options.HomeStorage == StorageType.EXTERNAL_STORAGE) {
				f = getRootFolder() + "/Music";
			} else {
				f = Environment.getExternalStoragePublicDirectory(
						Environment.DIRECTORY_MUSIC).getAbsolutePath();
			}
			break;
		}

		return f;
	}

}
